<template>
  <div>
    <FilterTable
     ref="table"
     :columns="columns"
     :params="fetchParams"
     :datakey="'records'"
     :isMemoryPagination="true"
     :isSelectColumn="false"
     :onRowSelectChange="selectChange"
     columnsRef="jzySeries"
     :onColumnsChange="columns => this.columns = columns"
    >
      <template slot="controls" slot-scope="props">
        <el-button size="small" type="primary"  @click="dialogHandle()">新增</el-button>
      </template>
    </FilterTable>
    <div class="dialogBottmView" >
      <el-button @click="closeDrawer(1)">关 闭</el-button>
      <el-button :loading="saveLoading" @click="closeDrawer(2)" type="primary">确 定</el-button>
    </div>
    <BaseDialog
      :visible.sync="issearch"
      :title="'查询配件'"
      destroyOnClose
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
    >
    <prats  @close="dealercloseHandler" />
    </BaseDialog>
  </div>
</template>

<script>
  import prats from './prats'
  import { dictionary } from '@/utils/dictMixin';
  import { region } from '@/api/cms-common/region';
  import { getPartOrdertypedetilList,dosave } from '@/api/partsmanage/partordertype/index';
  export default {
    mixins: [dictionary,region],
    name: 'partordertypedetil',
    components: {
      prats
    },
    props: {
      rowData: {
        type: Object,
        default: () => ({})
      }
    },
    data() {
      this.saveData=null;
      return {
        list:[],
        columns: this.createTableColumns(),
        fetchParams: {},
        tableDate:{
          addList:[],
          delList:[],
          editList:[]
        },
        isSave:false,
        records:'',
        issearch:false, //配件弹框控制
        saveLoading: false,
      };
    },
    created() {
      this.partOrderTypeList()
    },
    methods: {
      closeDrawer(type){
        if (type===1){
          this.$confirm('确认关闭窗口吗？您所做的操作将不会被保存！', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          })
            .then(() => {
              this.$emit('close')
            })
        }else{
          const flag= this.$refs.table.GET_REQUIRED_ERROR()
          if (flag){
          }else{
           const add= this.$refs.table.GET_INSERT_ROWS()
           const del=this.$refs.table.GET_DELETE_ROWS()
           const edit=this.$refs.table.GET_UPDATE_ROWS()
            add.forEach(item=>{
              item.bjpjsx= item.bjpjsx1
              this.tableDate.addList.push(item)
            })
            del.forEach(item=>{
              item.bjpjsx= item.bjpjsx1
              this.tableDate.delList.push(item)
            })
            edit.forEach(item=>{
              item.bjpjsx= item.bjpjsx1
              this.tableDate.editList.push(item)
            })
            console.info(this.tableDate)
            console.info('保存的数据')
           this.saveLoading=true
           dosave(this.rowData.id,this.tableDate).then(res=>{
             if (res.resultCode === 200) {
               this.$notify.success({title: '提示信息',message:'保存成功!'});
               this.saveLoading=false
               // 重新加载
               this.$emit('close')
             }else {
               this.$notify.error({title: '提示信息',message:res.errMsg||'保存失败!'});
               this.$emit('close')
             }
          })
          }
        }
      },
      delete(row){
        this.list.forEach((item,index)=>{
          if (item.bjxmbh === row.bjxmbh){
            this.list.splice(index,1)
          }
        })
        this.$refs.table.EXECUTE_DELETE([row])
      },
      partOrderTypeList(){
        getPartOrdertypedetilList(this.rowData.id).then(res=>{
          this.$refs.table.SET_TABLE_DATA(res.data)
          this.list=res.data
        })
      },
      // 关闭配件选择框
      dealercloseHandler(val){
        if (val){
          const addArr = val.filter(i => !this.list.some(j => j.partInfoId === i.id))
            .map(i => {
              return {
                partInfoId: i.id,
                bjxmbh: i.bjxmbh,
                xtqybz:null,
                bjzwmc: i.bjzwmc,
                bjpjsx: this.createDictText(i.bjpjsx, '5000'),
                bjpjsx1:i.bjpjsx
              }
            })
          addArr.forEach(item=>{
            this.list.push(item)
          })
          this.$refs.table.EXECUTE_INSERT(addArr)
        }
        this.issearch=false;
      },
      dialogHandle(){
        this.issearch=true;
      },
      selectChange(val) {
        console.log('val', val);
        this.rowSelectData = val;
      },
      createTableColumns () {
        return [
          {
            title: '操作',
            dataIndex: 'column-action',
            width: 60,
            fixed: 'left',
            render: (props) => {
              return (
                <div>
                <el-button size="mini" type="text" onClick={() => this.delete(props.row)}>
              删除
              </el-button>
              </div>
            );
            }
          },
          {
            title: '序号',
            dataIndex: 'index',
            width: 70,
            sorter: true,
            render: props => {
              return <span>{props.row.index + 1}</span>;
            }
          },
          {
            title: '配件代码',
            dataIndex: 'bjxmbh',
            sorter: true,
            filter: true,
            filterType: 'input',
          },
          {
            title: '配件名称',
            dataIndex: 'bjzwmc',
            sorter: true,
            filter: true,
            filterType: 'input',
          },
          {
            title: '适用标记',
            dataIndex: 'xtqybz',
            sorter: true,
            editType:'select',
            editRequired: true,
            editable: true,
            editItems:this.createDictList('1004'),
            filter: true,
            filterType: 'checkbox',
            filterItems:this.createDictList('1004')
          },
          {
            title: '配件属性',
            dataIndex: 'bjpjsx',
            sorter: true,
            filter: true,
            filterType: 'checkbox',
            filterItems:this.createDictList('5000'),
            render: props => {    //编码转汉字
              return <span>{this.createDictText(props.row.bjpjsx, '5000')}</span>;
            }
          }
        ]
      },
    }
  };
</script>

<style scoped>

</style>
